﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Helper.Text
{
    public class Coding
    {
        static Coding _text = null;
        string EncryptionKey = "DMail2.0.0.1";
        public static  Coding Instance
        {
            get
            {
                if (_text == null)
                {
                    _text = new Coding();
                }
                return _text;
            }
        }

        /// <summary>
        /// 将十六进制的字符串转为汉字:形如\u6563
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public  string HexToString(string str)
        {
            int nIndex = 0;
            while (true)
            {
                try
                {
                    nIndex = str.IndexOf("\\u", nIndex);
                    if (nIndex != -1)
                    {
                        string starget = str.Substring(nIndex + 2, 4);
                        long nvalue = Int16.Parse(starget, System.Globalization.NumberStyles.HexNumber);
                        byte[] b = BitConverter.GetBytes(nvalue);
                        string svalue = Encoding.Unicode.GetString(b);
                        svalue = svalue.Substring(0, 1);
                        str = str.Replace("\\u" + starget, svalue);
                    }
                    else
                    {
                        break;
                    }
                }
                catch (Exception)
                {
                    nIndex += 4;
                }
            }

            return str;
        }

        public string GetMD5(string input)
        {
            if (String.IsNullOrEmpty(input))
            {
                return "";
            }
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        public string putstr(string message, string startStr, string endStr, int startIndex)
        {
            if (startIndex < 0)
            {
                return "-1";
            }
            if (message.Length < startIndex)
            {
                return "-1";
            }
            string str = message.Substring(startIndex, message.Length - startIndex);
            int index = str.IndexOf(startStr);
            if (index < 0)
            {
                return "-1";
            }
            index += startStr.Length;
            str = str.Substring(index, str.Length - index);
            int length = str.IndexOf(endStr);
            if (length < 0)
            {
                return "-1";
            }
            return str.Substring(0, length);
        }

    }
}
